package com.example.micrweb.front.settings;

import com.example.micrweb.front.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/14 下午 03:57
 * @version: v1.0
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Value("${jwt.secret}")
    private String jwtSecret;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        TokenInterceptor tokenInterceptor = new TokenInterceptor(jwtSecret);
        String[] paths = {"/v1/user/realName","/v1/product/info",""};
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns(paths);
    }

    /* 处理全局跨域*/
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        System.out.println();
        // 拦截地址
        registry.addMapping("/**")
                // 可跨域的域名
                .allowedOriginPatterns("*","http://172.220.3.92:8081") //"http://localhost:8081",
                // 允许跨域的请求方式
                .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
                // true表示可以使用cookie
                .allowCredentials(true)
                // 指定请求的有效期
                .maxAge(3600)
                // 允许跨域的请求头
                .allowedHeaders("*");
    }
}
